package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/*
设置事务隔离级别；
 */
public class Demo12 {
    public static void main(String[] args) {
        try (Connection connection=DBUtil.getConnection();){
            // 设置事务隔离级别
            connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
            connection.setAutoCommit(false);// 关闭自动提交;
            String sql="INSET INTO USER(username,password,nickname,age) " +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,"超哥");
            ps.setString(2,"123456");
            ps.setString(3,"伟哥");
            ps.setInt(4,22);
            int num=ps.executeUpdate();
            System.out.println(num>0?"插入成功":"插入失败");
            connection.commit();
        } catch (SQLException e) {
            //connection.rollback();没必要写；
            e.printStackTrace();
        }
    }
}
